<?php


namespace App\Api\Repositories;

use App\Api\Models\CmcGoodsCart;
use App\Api\Models\Goods;

class GoodsCartRepositories
{
    //添加商品购物车
    static public function addGoodsCart(int $gid,int $uid)
    {
        //查询商品
        $allow_field = [
            'goods_name',
            'goods_image',
            'price',
            'goods_id'
        ];
        $data = Goods::getCartGoods($gid,$allow_field);
        //添加到购物车
        $data['goods_price'] = $data['price'];
        $data['uid'] = $uid;
        unset($data['price']);
        $results = CmcGoodsCart::addCart($data);
        if($results){
            return true;
        }else{
            return false;
        }
    }
    //验证是否有数据
    static public function haveData($gid,$uid)
    {
        return CmcGoodsCart::onceCart($gid,$uid);
    }

    //获取购物车数据
    static public function getCartAll($uid)
    {
        return CmcGoodsCart::allCart($uid);
    }
    //获得购物车总价
    static public function getCartTotal($cart)
    {
        if(!is_array($cart)){
            return false;
        }
        $money = [0];
        foreach ($cart as $item){
            $money[] = $item[0] * CmcGoodsCart::getCartprice($item[1]);
        }
        return array_sum($money);
    }
}